package de.bastian.hinterleitner.tools;

import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class Entry extends DataBaseEntry{

	private String name = "not found";
	private Category category = new Category("none", null, -1);
	private double value = 0;
	private Unit unit;
	private int year;
	private int month;
	private int day;
	private int id = -1;
	private int repeatableReference = -1;
	
	/**
	 * so far only used for categories!
	 */
	private Drawable drawable;
	
	public static int sorting = EntrySorter.SORT_BY_DATE;

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public Drawable getDrawable(){
		return drawable;
	}
	
	public void setDrawable(Drawable d){
		drawable = d;
	}
	
	public double getValue() {
		return Math.rint(value * 100) / 100;
	}
	
	public void setValue(double v){
		value = v;
	}
	
	public double getValueAs(String unit, Context c){
		if(this.unit==null || this.unit.getSign().equals(unit)){
			return value;
		}else{
			return this.unit.convert(value, Unit.get(unit, c));
		}
	}
	
	public Unit getUnit(){
		return unit;
	}
	
	public void setUnit(Unit unit){
		this.unit = unit;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category cat) {
		category = cat;
	}

	public int getYear() {
		return year;
	}
	
	public int getMonth() {
		return month;
	}
	
	public int getDay() {
		return day;
	}

	public void setDate(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	public void setDate(Date d){
		if(d.getYear()<1000){
			year = d.getYear()+1900;
		}else{
			year = d.getYear();
		}
		month = d.getMonth()+1;
		day = d.getDate();
	}
	
	public Date getDate(){
		return new Date(year-1900, month-1, day);
	}
	
	public int getRepeatableReference(){
		return repeatableReference;
	}
	
	public void setRepeatableReference(int ref){
		this.repeatableReference = ref;
	}

	public Entry(Entry e) {
		year = e.year;
		month = e.month;
		day = e.day;
		name = e.name;
		value = e.value;
		id = e.id;
		category = e.category;
		drawable = e.drawable;
		repeatableReference = e.repeatableReference;
		unit = e.unit;
	}
	
	public Entry(Entry e, int id) {
		year = e.year;
		month = e.month;
		day = e.day;
		name = e.name;
		value = e.value;
		this.id = id;
		category = e.category;
		drawable = e.drawable;
		repeatableReference = e.repeatableReference;
		unit = e.unit;
	}

	public Entry(String name, double value, int id) {
		this.id = id;
		year = Calendar.getInstance().get(Calendar.YEAR);
		month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		this.name = name;
		this.value = value;
	}

	public Entry(String name) {
		this.name = name;
		value = 0;
	}

	@Override
	public String toString() {
		return name;
	}
	
	public String toFormattedString(){
		return "id:" + id + " - " + day + "." + month + "." + year + "\t"
		+ name + ": " + (Math.rint(value * 100) / 100)+(unit==null?"":unit.getSign());
	}
}
